<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="结算单号" prop="businessNum">
        <el-input
          v-model="queryParams.businessNum"
          placeholder="请输入结算单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="settlementList" >
      <el-table-column type="index" label="序号" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
      <el-table-column label="结算单号" align="center" prop="businessNum" width="150"/>
      <el-table-column label="园区" align="center" prop="area" width="120"/>
<!--      <el-table-column label="${comment}" align="center" prop="vendorId" />-->
      <el-table-column label="供应商" align="center" prop="vendorName" />
      <el-table-column label="结算日期" align="center" prop="settlementTime" width="120"/>
      <el-table-column label="结算总金额" align="center" prop="money" width="120">
        <template v-slot="{row}">
          <span>{{row.money | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="120">
        <template v-slot="{row}">
          <span v-if="row.status==0">待确认</span>
          <span v-if="row.status==1">已确认</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" width="120"/>
      <el-table-column label="创建日期" align="center" prop="createTime" width="150"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.status==0"  type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">确认</el-button>
          <el-button size="mini" v-if="scope.row.status==0"  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleBalanceDetail(scope.row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="园区:" prop="area">
          {{form.area}}
        </el-form-item>
        <el-form-item label="结算单号:" prop="businessNum">
          {{form.businessNum}}
        </el-form-item>
        <el-form-item label="供应商:" prop="vendorName">
          {{form.vendorName}}
        </el-form-item>
        <el-form-item label="结算总金额:" prop="money">
          {{form.money | currency}}
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" >
            <el-option label="待确认" value="0" ></el-option>
            <el-option label="已确认" value="1" ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSettlement,
  getSettlement,
  delSettlement,
  addSettlement,
  updateSettlement,
  createSettlement
} from "@/api/settlement/supplier";

export default {
  name: "Settlement",
  dicts:['user_area'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      settlementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessNum: null,
        area: null,
        vendorId: null,
        vendorName: null,
        settlementTime: null,
        money: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listSettlement(this.queryParams).then(response => {
        this.settlementList = response.rows;
        this.total = response.total;
      }).finally(()=>{
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        businessNum: null,
        area: null,
        vendorId: null,
        vendorName: null,
        settlementTime: null,
        money: null,
        createBy: null,
        createTime: null,
        status: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$confirm('请确认是否生成供应商结算单').then(res=> {
        createSettlement().then(res=>{
          this.getList()
        })
      })
      // this.reset();
      // this.open = true;
      // this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$confirm('是否确认该供应商结算单').then(res=> {
        row.status=1
        updateSettlement(row).then(response => {
          this.$modal.msgSuccess("确认成功");
          // this.open = false;
          this.getList();
        });
      })
      // this.reset();
      // const id = row.id || this.ids
      // getSettlement(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改结算单";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateSettlement(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          // if (this.form.id != null) {
          //
          // } else {
          //   addSettlement(this.form).then(response => {
          //     this.$modal.msgSuccess("新增成功");
          //     this.open = false;
          //     this.getList();
          //   });
          // }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该供应商结算单').then(function() {
        return delSettlement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/settlement/export', {
        ...this.queryParams
      }, `settlement_${new Date().getTime()}.xlsx`)
    },
    handleBalanceDetail(row){
      this.$router.push({
        path:'/settlement/supplier/detail',
        query:{
          businessNum:row.businessNum
        }
      })
    }
  }
};
</script>
